package com.aacar.controller.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aacar.controller.ValidController;
import com.aacar.dto.Header;
import com.aacar.dto.MessageDto;
import com.aacar.service.MemberService;
import com.aacar.service.MessageService;
import com.aacar.utils.ErrorCodeConsField;
import com.aacar.utils.ErrorInfoUtil;
import com.aacar.utils.Pphconfig;
import com.aacar.utils.ResultInfoUtil;
import com.aacar.vo.CommonVo;
import com.aacar.vo.MessageVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;

/**
 * 消息控制类
 * @author wanghui
 *
 */
@RestController
@RequestMapping("/message")
public class MessageController extends ValidController{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private MemberService memberService;
	
	/**
	 * 发送短信
	 * @param userDto
	 * @return
	 */
	@RequestMapping(value = "/sendShortMessage/0", method = RequestMethod.POST)
    @ResponseBody
	public Object sendShortMessage(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody MessageDto messageDto) {
		Header header = null;
		if(null != mobilehead) {
			header = JSON.parseObject(mobilehead, Header.class);
		}
		CommonVo comv = this.memberService.getCountByPhoneNo(messageDto.getPhoneNo());
		//1.表示忘记密码获取验证码
		if("1".equals(messageDto.getFlag())) {
	    	if(comv.isSuccess()) {//该用户不存在
	    		return sendMessage(messageDto,header.getService());
	    	}else {
	    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10308, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10308),header.getService());
	    	}
		}
		//表示注册
		if("0".equals(messageDto.getFlag())) {
	    	if(comv.isSuccess()) {
	    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10300, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10300),header.getService());
	    	}
		}
		return sendMessage(messageDto,header.getService());
	}
	
	//发送短信
	private Object sendMessage(MessageDto messageDto,String serviceCode) {
		if(StringUtils.isNullOrEmpty(messageDto.getPhoneNo())){
    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10301, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10301),serviceCode);
    	}
		MessageVo mv = messageService.sendShortMessage(messageDto);
		if(mv.isSuccess()) {
			return ResultInfoUtil.setSuccessInfo(mv,serviceCode);
		}else {
			return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10305, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10305),serviceCode);
		}
	}
	
	/**
	 * 校验短信
	 * @param userDto
	 * @return
	 */
	@RequestMapping(value = "/validatorShortMessage/0", method = RequestMethod.POST)
    @ResponseBody
	public Object validatorShortMessage(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody MessageDto messageDto) {
		logger.info("request:/validatorShortMessage/0" + JSONObject.toJSONString(messageDto));
		Header header = null;
		if(null != mobilehead) {
			header = JSON.parseObject(mobilehead, Header.class);
		}
		if(StringUtils.isNullOrEmpty(messageDto.getPhoneNo())){
    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10301, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10301),header.getService());
    	}
		MessageVo mv = messageService.validatorShortMessage(messageDto);
		if(mv.isSuccess()) {
			return ResultInfoUtil.setSuccessInfo(mv,header.getService());
		}else {
			return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10305, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10305),header.getService());
		}
	}
	
	/**
	 * 获取图片验证码
	 * @param userDto
	 * @return
	 */
	@RequestMapping(value = "/retrieveImageCode/0", method = RequestMethod.POST)
    @ResponseBody
	public Object retrieveImageCode(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody MessageDto messageDto) {
		Header header = null;
		if(null != mobilehead) {
			header = JSON.parseObject(mobilehead, Header.class);
		}
		String code = this.messageService.retrieveImageCode(messageDto.getPhoneNo(),messageDto.getType());
		return ResultInfoUtil.setSuccessInfo(code,header.getService());
	}
	
}
